<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/7/23
 * Time: 14:00
 */
namespace App\Http\Controllers\Admin\Order;
use App\Http\Controllers\AdminController;

use \App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderController extends AdminController
{

    public function index(Request $request)
    {
        $data = array();
        $data['search'] = [];
        $data['search']['status'] = '';
        $list = DB::table('order')
                    ->leftJoin('user','order.uid','=','user.id')
                    ->select('order.*','user.name');
        if(!empty($_GET['status']))
        {
            $list = $list->where('order.status',$_GET['status']);
            $data['search']['status'] = $_GET['status'];
        }else if(key_exists('status',$_GET)){
            $data['search']['status'] = 0;
        }
        if(!empty($_GET['type']))
        {
            $list = $list->where('order.type',$_GET['type']);
            $data['search']['type'] = $_GET['type'];
        }else if(key_exists('type',$_GET)){
            $data['search']['type'] = 0;
        }
        if(!empty($_GET['name']))
        {
            $list = $list->where('user.name',"like","%".$_GET['name']."%");
            $data['search']['name'] = $_GET['name'];
        }
        $list = $list->paginate(15);
        foreach ($list as $k => $v)
        {
            if(!empty($v->pid))
            {
                $getPidName = DB::table('user')
                                    ->select('name')
                                    ->where('id',$v->pid)
                                    ->first();
                $list[$k]->pidname = !empty($getPidName->name) ? $getPidName->name : '';
            }else{
                $list[$k]->pidname = '';
            }
        }
        $data['list'] = $list;
        return \Helpers::showViewCURD('Admin.Order.index',3,'',$data);
    }
}